package com.ysqcould.Community.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ysqcould.Community.domain.Charity;
import com.ysqcould.Community.mapper.CharityMapper;
import com.ysqcould.Community.service.ICharityService;
import com.ysqcould.common.utils.DateUtils;
import com.ysqcould.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 慈航公益Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-30
 */
@Service
public class CharityServiceImpl implements ICharityService
{
    @Autowired
    private CharityMapper charityMapper;

    /**
     * 查询慈航公益
     * 
     * @param id 慈航公益主键
     * @return 慈航公益
     */
    @Override
    public Charity selectCharityById(Long id)
    {
        final Charity charity = charityMapper.selectCharityById(id);
        if (charity.getAddress().contains("name")){
            final JSONObject object = JSON.parseObject(charity.getAddress());
            if (ObjectUtil.isNotEmpty(object)){
                charity.setDetailedAddress(object.getString("name"));
            }
        }else {
            charity.setDetailedAddress(charity.getAddress());
        }
        return charity;
    }

    /**
     * 查询慈航公益列表
     * 
     * @param charity 慈航公益
     * @return 慈航公益
     */
    @Override
    public List<Charity> selectCharityList(Charity charity)
    {
        List<Charity> charityList = charityMapper.selectCharityList(charity);
        charityList.stream().filter(p->StringUtils.isNotEmpty(p.getPcUrl())).forEach(p->{
            String[] strings = p.getPcUrl().split(",");
            p.setPcUrl(strings[0]);
        });
        return  charityList;
    }

    /**
     * 新增慈航公益
     * 
     * @param charity 慈航公益
     * @return 结果
     */
    @Override
    public int insertCharity(Charity charity)
    {
        charity.setCreateTime(DateUtils.getNowDate());
        return charityMapper.insertCharity(charity);
    }

    /**
     * 修改慈航公益
     * 
     * @param charity 慈航公益
     * @return 结果
     */
    @Override
    public int updateCharity(Charity charity)
    {
        charity.setUpdateTime(DateUtils.getNowDate());
        return charityMapper.updateCharity(charity);
    }

    /**
     * 批量删除慈航公益
     * 
     * @param ids 需要删除的慈航公益主键
     * @return 结果
     */
    @Override
    public int deleteCharityByIds(Long[] ids)
    {
        return charityMapper.deleteCharityByIds(ids);
    }

    /**
     * 删除慈航公益信息
     * 
     * @param id 慈航公益主键
     * @return 结果
     */
    @Override
    public int deleteCharityById(Long id)
    {
        return charityMapper.deleteCharityById(id);
    }

    @Override
    public int changeIsCarouselStatus(Charity charity) {
        Charity charityById = charityMapper.selectCharityById(charity.getId());
        charityById.setIsCarousel(charity.getIsCarousel());
        return charityMapper.updateCharity(charity);
    }

    @Override
    public int changeShowStatus(Charity charity) {
        Charity charityById = charityMapper.selectCharityById(charity.getId());
        charityById.setShowStatus(charity.getShowStatus());
        return charityMapper.updateCharity(charity);
    }

    @Override
    public int updateStatus(Charity charity) {
        Charity charityById = charityMapper.selectCharityById(charity.getId());
        charityById.setStatus(charity.getStatus());
        return charityMapper.updateCharity(charity);
    }
}
